package com.xhl.controller;

import com.xhl.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Slf4j
public class LoginController {

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @PostMapping("/login")
    @ResponseBody
    public String login(@Validated User user){  //, BindingResult result



/*        if(result.hasErrors()){
            log.info("有校验错误信息");
            return returnObject(result).toString();
        }*/

        return user.toString();
    }


    /*private Object returnObject(BindingResult bindingResult) {
        List<FieldErrorVO> fieldErrorVos=new ArrayList<FieldErrorVO>();
        StringBuilder msg=new StringBuilder();
        try {
            if(bindingResult.hasErrors()) {
                List<FieldError> fieldErrors=bindingResult.getFieldErrors();

                FieldErrorVO fieldErrorVo;
                for (FieldError fieldError:fieldErrors) {
                    fieldErrorVo=new FieldErrorVO(fieldError.getField(),fieldError.getDefaultMessage());
                    log.info("校验错误字段信息={}",fieldErrorVo);
                    fieldErrorVos.add(fieldErrorVo);
                    msg.append(", ").append(fieldError.getDefaultMessage());
                }
                msg.deleteCharAt(0);
            }
        } catch (Throwable  tx) {
            log.error("转换校验参数异常",tx);
        }
        Map<String, Object> msgMap=new HashMap<String, Object>();
        msgMap.put("success", false);
        msgMap.put("code", "1000");
        msgMap.put("msg", fieldErrorVos.get(0).getMsg());
        return msgMap;
    }*/

}
